package com.lanou.dllo.vc.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.entity.GroupUserName;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/12/2.
 */
public class GroupAdapter extends BaseAdapter {

    private List<String> usernames;
    private LayoutInflater inflater;
    private Context context;
    public  static   boolean[] isCheckedArray;
    private List<GroupUserName> datas;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==1) {
                notifyDataSetChanged();
            }
            return false;
        }
    });

    /** 是否为单选 */
    private boolean isSignleChecked;
    public GroupAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);

    }

    public List<GroupUserName> getCheckedName(){
        List<GroupUserName> checkeds= new ArrayList<>();
        for (GroupUserName u: datas){
            if (u.isChecked()){
                checkeds.add(u);
            }
        }
        return checkeds;
    }

    public void addData(List<String> usernames) {
        this.usernames = usernames;
        datas = new ArrayList<>();
        for (String u: usernames){
            datas.add(new GroupUserName(false,u));
        }

        Message message = new Message();
        message.what = 1;

        handler.sendEmptyMessage(message.what);

    }

    @Override
    public int getCount() {
        return usernames != null && usernames.size() > 0 ? usernames.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return usernames.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private String username;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_friendlist, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        username = usernames.get(position);

        GroupUserName groupUserName = datas.get(position);

        holder.friendname.setText(username);

        if (holder.checkBox!= null) {

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                datas.get(position).setIsChecked(isChecked);
                

            }
        });

        }


        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.isSelect)
        CheckBox checkBox;
        @Bind(R.id.friendname)
        TextView friendname;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
